import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

/**
 * 
 * @author Sarah
 *
 */
public class Server
{
	 public static void main(String[] args) throws Exception 
	 {
	        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
	        server.createContext("/index.html", new MyHandler());
	        server.setExecutor(null); // default executor
	        server.start();
	 }

	 static class MyHandler implements HttpHandler 
	 {
		 public void handle(HttpExchange exchange) throws IOException 
		 {
	            String response = readFile(exchange.getRequestURI().toString());
	            
	            // POST GET exchange.getRequestMethod();
	            exchange.sendResponseHeaders(200, response.length());
	            OutputStream os = exchange.getResponseBody();
	            os.write(response.getBytes());
	            os.close();
		 }
	        
		 private String readFile(String filename)
		 {
			 BufferedReader br = null;
			 String response = "";
			 try 
			 {
				String sCurrentLine;		 
				br = new BufferedReader(new FileReader("../frontend/" + filename));
		 
				while ((sCurrentLine = br.readLine()) != null) 
					response += sCurrentLine;
				br.close();
				return response;
			 } catch (IOException e) 
			 {
				e.printStackTrace();
				return "";
			} 
		 }
	 }
} 